<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Add a task to be run in parallel</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gearmanclient.addservers.html">GearmanClient::addServers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="gearmanclient.addtaskbackground.html">GearmanClient::addTaskBackground</a></div>
 <div class="up"><a href="class.gearmanclient.html">GearmanClient</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="gearmanclient.addtask" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">GearmanClient::addTask</h1>
  <p class="verinfo">(PECL gearman &gt;= 0.5.0)</p><p class="refpurpose"><span class="refname">GearmanClient::addTask</span> &mdash; <span class="dc-title">Add a task to be run in parallel</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-gearmanclient.addtask-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.gearmantask.html" class="type GearmanTask">GearmanTask</a></span> <span class="methodname"><strong>GearmanClient::addTask</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$function_name</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$workload</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&$context</code></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$unique</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Adds a task to be run in parallel with other tasks.  Call this method for all the tasks
   to be run in parallel, then call <span class="methodname"><a href="gearmanclient.runtasks.html" class="methodname">GearmanClient::runTasks()</a></span> to 
   perform the work.  Note that enough workers need to be available for the tasks to all
   run in parallel.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-gearmanclient.addtask-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">function_name</code></em></dt>

     <dd>

      <p class="para">
       由程序自动执行的已注册函数。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">workload</code></em></dt>

     <dd>

      <p class="para">
       被处理的序列化数据。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">context</code></em></dt>

     <dd>

      <p class="para">
       与任务关联的应用程序上下文。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">unique</code></em></dt>

     <dd>

      <p class="para">
       用于标识特定任务的唯一性 ID。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-gearmanclient.addtask-returnvalues">
  <h3 class="title">返回值</h3>
 <p class="para">
   A <a href="class.gearmantask.html" class="classname">GearmanTask</a> object or <strong><code>FALSE</code></strong> if the task could not be added.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-gearmanclient.addtask-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4222">
    <p><strong>Example #1 Basic submission of two tasks</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">#&nbsp;Create&nbsp;our&nbsp;gearman&nbsp;client<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">GearmanClient</span><span style="color: #007700">();&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;add&nbsp;the&nbsp;default&nbsp;job&nbsp;server<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">();&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;set&nbsp;a&nbsp;function&nbsp;to&nbsp;be&nbsp;called&nbsp;when&nbsp;the&nbsp;work&nbsp;is&nbsp;complete<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCompleteCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"complete"</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;add&nbsp;a&nbsp;task&nbsp;to&nbsp;perform&nbsp;the&nbsp;"reverse"&nbsp;function&nbsp;on&nbsp;the&nbsp;string&nbsp;"Hello&nbsp;World!"<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addTask</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World!"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"1"</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;add&nbsp;another&nbsp;task&nbsp;to&nbsp;perform&nbsp;the&nbsp;"reverse"&nbsp;function&nbsp;on&nbsp;the&nbsp;string&nbsp;"!dlroW&nbsp;olleH"<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addTask</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"!dlroW&nbsp;olleH"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"2"</span><span style="color: #007700">);&nbsp;<br /><br /></span><span style="color: #FF8000">#&nbsp;run&nbsp;the&nbsp;tasks<br /></span><span style="color: #0000BB">$gmclient</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">runTasks</span><span style="color: #007700">();&nbsp;<br /><br />function&nbsp;</span><span style="color: #0000BB">complete</span><span style="color: #007700">(</span><span style="color: #0000BB">$task</span><span style="color: #007700">)&nbsp;<br />{&nbsp;<br />&nbsp;&nbsp;print&nbsp;</span><span style="color: #DD0000">"COMPLETE:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$task</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unique</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$task</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;&nbsp;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
COMPLETE: 2, Hello World!
COMPLETE: 1, !dlroW olleH
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4223">
    <p><strong>Example #2 Basic submission of two tasks with passing application context</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$client&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">GearmanClient</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$client</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addServer</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">#&nbsp;set&nbsp;a&nbsp;function&nbsp;to&nbsp;be&nbsp;called&nbsp;when&nbsp;the&nbsp;work&nbsp;is&nbsp;complete<br /></span><span style="color: #0000BB">$client</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCompleteCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse_complete"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">#&nbsp;Add&nbsp;some&nbsp;tasks&nbsp;for&nbsp;a&nbsp;placeholder&nbsp;of&nbsp;where&nbsp;to&nbsp;put&nbsp;the&nbsp;results<br /></span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /></span><span style="color: #0000BB">$client</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addTask</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World!"</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$results</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"t1"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$client</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addTask</span><span style="color: #007700">(</span><span style="color: #DD0000">"reverse"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"!dlroW&nbsp;olleH"</span><span style="color: #007700">,&nbsp;&amp;</span><span style="color: #0000BB">$results</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"t2"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$client</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">runTasks</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">#&nbsp;The&nbsp;results&nbsp;should&nbsp;now&nbsp;be&nbsp;filled&nbsp;in&nbsp;from&nbsp;the&nbsp;callbacks<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$id&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">":&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'handle'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">",&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">[</span><span style="color: #DD0000">'data'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /><br />function&nbsp;</span><span style="color: #0000BB">reverse_complete</span><span style="color: #007700">(</span><span style="color: #0000BB">$task</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$results</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$results</span><span style="color: #007700">[</span><span style="color: #0000BB">$task</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">unique</span><span style="color: #007700">()]&nbsp;=&nbsp;array(</span><span style="color: #DD0000">"handle"</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$task</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">jobHandle</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"data"</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$task</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
t2: H.foo:21, Hello World!
t1: H:foo:22, !dlroW olleH
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-gearmanclient.addtask-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="gearmanclient.addtaskhigh.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::addTaskHigh()</a> - Add a high priority task to run in parallel</span></li>
    <li class="member"><span class="methodname"><a href="gearmanclient.addtasklow.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::addTaskLow()</a> - Add a low priority task to run in parallel</span></li>
    <li class="member"><span class="methodname"><a href="gearmanclient.addtaskbackground.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::addTaskBackground()</a> - Add a background task to be run in parallel</span></li>
    <li class="member"><span class="methodname"><a href="gearmanclient.addtaskhighbackground.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::addTaskHighBackground()</a> - Add a high priority background task to be run in parallel</span></li>
    <li class="member"><span class="methodname"><a href="gearmanclient.addtasklowbackground.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::addTaskLowBackground()</a> - Add a low priority background task to be run in parallel</span></li>
    <li class="member"><span class="methodname"><a href="gearmanclient.runtasks.html" class="methodname" rel="rdfs-seeAlso">GearmanClient::runTasks()</a> - Run a list of tasks in parallel</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="gearmanclient.addservers.html">GearmanClient::addServers</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="gearmanclient.addtaskbackground.html">GearmanClient::addTaskBackground</a></div>
 <div class="up"><a href="class.gearmanclient.html">GearmanClient</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
